package com.vecspace.statistics.counter;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**码表器管理单元。用于管理连续的多个码表
 * @version: 1.0
 * @author: wanghua
 */
public class Stopwatcher {
	
	public Stopwatcher() {
		
	}
	
	public Stopwatcher(int maxStopwatchs) {
		this.maxStopwatchs = maxStopwatchs;
	}
	
	private Queue<Stopwatch> stopwatchs = new ConcurrentLinkedQueue<>();
	private int maxStopwatchs = 100;
	
	/**开始一个新的码表，并将新码表加入队列中。
	 * 
	 * @return 
	 * @author: wanghua
	 */
	public Stopwatch newStopwatch(){
		Stopwatch stopwatch = new Stopwatch();
		stopwatchs.add(stopwatch);
		while(stopwatchs.size() > maxStopwatchs) {
			stopwatchs.poll();
		}
		stopwatch.startTicking();
		return stopwatch;
	}
	
	/**
	 *  
	 * @author: wanghua
	 */
	public List<Stopwatch> getStopwatchs(){
		Stopwatch[] array = new Stopwatch[0];
		array = stopwatchs.toArray(array);
		List<Stopwatch> list = new ArrayList<>();
		for(Stopwatch item : array) {
			list.add(item);
		}
		return list;
	}
}
